package de.graffiti.beans;

import javax.ejb.Remote;
import javax.ejb.SessionBean;
import org.apache.log4j.Logger;
import org.jboss.ejb.SessionContainer;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import de.graffiti.interfaces.LoginService;

@Remote
public class Login
{
	private static final Logger log = Logger.getLogger(Login.class.getName());
	LoginService loginService;
	private boolean logged = false;
	
	public Login(){
		DBConnection db = new DBConnection();
		loginService = (LoginService) db.getService("LoginServiceBean/remote", LoginService.class);
		log.info("###############Verbindung:" + loginService + "  #################");
		
	}
	
	String name = "";
	String password = "";
	Boolean checkAdmin;
	
	public String login()
	{
		log.info("######### verify Login #########");

		if (!loginService.verifyLogin(name, password)) {
			FacesMessage fm = new FacesMessage("Benutzername oder Passwort falsch!");
			FacesContext.getCurrentInstance().addMessage("Field is Empty", fm);
			return "false";
		}else{
			logged=true;
			checkadmin();
			log.info("######### " + checkAdmin + " #########");
			return "success";
		}
	}
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	public void setLogged(boolean logged)
	{
		this.logged = logged;
	}
	public boolean isLogged()
	{
		return logged;
	}
	
	public String register(){
		return "register";
	}
	
	public String editProfile(){
		return "editProfile";
	}
	
	public String logout(){
		setName("");
		return "logout";
	}
	
	public String adminview(){
		return "adminview";
	}
	
	public Boolean getCheckAdmin()
	{
		return checkAdmin;
	}
	public void setCheckAdmin(Boolean checkAdmin)
	{
		this.checkAdmin = checkAdmin;
	}
	
	public void checkadmin(){
		if (name.equalsIgnoreCase("admin")){
			checkAdmin = false;
		}else checkAdmin=true;
	}
	
}
